clear all
set mem 15g
set matsize 11000
set maxvar 32767 
set more off
global seed 1
set seed $seed


********************************************************************************
********************************************************************************
// CHANGE FOLDER DIRECTORY HERE
global path "PATH NAME/ReplicationMaterial"


** Open log file
log using "$path\log_replication_CCNS.log", replace


// CREATE FOLDERS
cd "$path"
qui {
 mata: st_numscalar("x", direxists("Table"))
 if (scalar(x)==0) mkdir Table
 
 mata: st_numscalar("x", direxists("Figure"))
 if (scalar(x)==0) mkdir Figure
 
 mata: st_numscalar("x", direxists("Dataset"))
 if (scalar(x)==0) mkdir Dataset
 
}

// DEFINE PATHS
global outpath "$path/Table/"
global figpath "$path/Figure/"
global datapath "$path/Dataset/"
global rawpath "$path/Rawdata/"	
global progpath "$path/Program/"	

cd "$progpath"


********************************************************************************
********************************************************************************
// GLOBALS/LOCALS/FUNCTIONS
global LHSlist = "cog" //   
global RHSlist = "hours" //  

global BASICCONTROLlist = "afqt_mom afqt_mom2 male birth_order hispanic black mother_teen mother_old mother_2024 mother_2529 mother_3034 hs_mom col_mom hs_ls_mom col_sm_mom col_pl_mom spouse_atbirth spouse_educ_atbirth lnfamsize north south west"

*global CONTROLlist = "$BASICCONTROLlist"
foreach lhs in $LHSlist {
	
	global CONTROLlist_`lhs' = "$BASICCONTROLlist age_`lhs' d_1988_`lhs' d_1990_`lhs' d_1992_`lhs' d_1994_`lhs' d_1996_`lhs' d_1998_`lhs' d_2000_`lhs' d_2002_`lhs' d_2004_`lhs' d_2006_`lhs' d_2008_`lhs' d_2010_`lhs' d_2012_`lhs'"
	
	global CONTROLlist_`lhs'_wage "$BASICCONTROLlist wage_mom_r afqtwage_mom_r age_`lhs' d_1988_`lhs' d_1990_`lhs' d_1992_`lhs' d_1994_`lhs' d_1996_`lhs' d_1998_`lhs' d_2000_`lhs' d_2002_`lhs' d_2004_`lhs' d_2006_`lhs' d_2008_`lhs' d_2010_`lhs' d_2012_`lhs'"
	
	global CONTROLlist_`lhs'_income "$BASICCONTROLlist ln_non_mom_income non_mom_income0 age_`lhs' d_1988_`lhs' d_1990_`lhs' d_1992_`lhs' d_1994_`lhs' d_1996_`lhs' d_1998_`lhs' d_2000_`lhs' d_2002_`lhs' d_2004_`lhs' d_2006_`lhs' d_2008_`lhs' d_2010_`lhs' d_2012_`lhs'"
	
}

global subsample  "fullsample"
global boot_reps = 1000
global Kmax = 100
global Kmain = 50
global robust_list="simple"								
global metric_list="gower"											
global linkage_list="wardslinkage"		
global metric="$metric_list"		// metric used to cluster (hierarchical cluster, used in 004_Cluster.do)
global linkage="$linkage_list"		// linkage used to cluster (hierarchical cluster, used in 004_Cluster.do)
local Kmax="$Kmax"
global K_list="$Kmain(1)$Kmain"



********************************************************************************
********************************************************************************
// RUN DICTIONARY FROM NLSY RAWDATA
do "000_cnlsy_controlvariables_readinraw.do"
do "000_CNLSY79_outcomescognitive_readinraw.do"
do "000_nlsy_childresidencestatus_readinraw.do"
do "000_nlsy_maternalcontrolvariables_readinraw.do"
do "000_nlsy_maternalnoncognitive_readinraw.do"
do "000_nlsy_maternalweekhourswork_readinraw.do"
do "000_nlsy_maternalweekworkstatus_readinraw.do"
*/


********************************************************************************
********************************************************************************
// CREATE MAIN DATASET 
do "001_organize_maindata_child_mother.do"

* output: dta file: 001_nlsy_child_mother_cog.dta


********************************************************************************
********************************************************************************
// CREATE Figure 1 - Created in Latex


********************************************************************************
********************************************************************************
// CREATE Figure 2 after estimation of het results by mother's AFQT, see code line below


********************************************************************************
********************************************************************************
// CREATE TABLE 1 - Summary Stats 
do "002_summarize_stats.do"

* output: tex file: 002_TableSummaryStats.tex


********************************************************************************
********************************************************************************
// CREATE Figure 3 - Bunching by Quartile of Mother's AFQT
do "002_BunchingPlots.do"

* output: pdf file: 002_BunchingCDF_hours_AFQT.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 4 - Discontinuity at L=0
do "002_DiscPlots.do"

* output: pdf file: 002_Disc_Xhours_Zafqt_mom_400_onlyAtZero.pdf and 
*002_Disc_Xhours_Zspouse_atbirth_400_onlyAtZero.pdf


********************************************************************************
********************************************************************************
// CREATE Cluster sample for non parametric control
do "003_cluster.do"

* output: dta file: 003_cluster_fullsample_cog.dta


********************************************************************************
********************************************************************************
// CREATE Figure 5 - Discontinuity at L=0 with and without controls
do "003_DiscPlots_residual.do"

* output: pdf file: 002_Disc_Xhours_Zcog_400_onlyAtZero.pdf and 
*002_Disc_Xhours_ZRes_cog_400_onlyAtZero.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 6 - Heterogeneity in the Distribution of L across cluster
do "003_ClusterRank.do"

* output: pdf file: ClusterRank.pdf


********************************************************************************
********************************************************************************
// CREATE Table 2 - Homogenous Results
do "004_hom_results.do"
do "004_hom_results_BOOT.do"
do "005_table_hom_results.do"

* output: dta files: 004_hom_results_fullsample_cog_simple_K50.dta, 
* 004_hom_results_fullsample_cog_simple_K50BOOT.dta
* final output: tex file: 005_hom_estimates_fullsample_hours_K50.tex


********************************************************************************
********************************************************************************
// CREATE Table 3 - Heterogenous Results by AFQT

global BETA_control_list "afqt_mom hhours hrsafqt" 
global DELTA_control_list "afqt_mom" 
global subsample = "fullsample"
local subsample  "$subsample"
global modelname "table3_main"

do "004_het_beta3_delta1_results.do"
do "004_het_beta3_delta1_results_BOOT.do"
do "005_table_het_beta3_delta1_results.do"

* output: dta files: 004_het_beta3_delta1_results_fullsample_table3_main_cog_simple_K50.dta, 
* 004_het_beta3_delta1_results_fullsample_table3_main_cog_simple_K50BOOT.dta
* final output: tex file: 005_het_beta3_delta1_estimates_fullsample_table3_main_hours_K50.tex


********************************************************************************
********************************************************************************
// CREATE Figure 2 after estimation of het results by mother's AFQT
global BETACONTROLlist_hrs_cog_linint  "hhours afqt_mom hrsafqt" 
global DELTACONTROLlist_hrs_cog_linint  "afqt_mom" 

do "005_figures_skillpaths_beta_delta_het.do"

* output: pdf file: total_skills_high_low_beta_delta_het_cog_linint_symmetric_all_quartiles.pdf


********************************************************************************
********************************************************************************
// CREATE Table 4 - Heterogenous Results by AFQT and wage

global BETA_control_list "afqt_mom wage_mom_r hhours hrsafqt hrswage_r" 
global DELTA_control_list "afqt_mom wage_mom_r" 
global subsample "wagebefore"
local subsample = "$subsample"
global modelname = "table5_notrunc"

do "003_cluster.do" // create new clusters adding wage variables as controls
	
do "004_het_beta5_delta2_results.do"
do "004_het_beta5_delta2_results_BOOT.do"
do "005_table_het_beta5_delta2_results.do"

* output: dta files: 003_cluster_wagebefore_cog.dta
* 004_het_beta5_delta2_results_wagebefore_table5_notrunc_cog_simple_K50.dta 
* 004_het_beta5_delta2_results_wagebefore_table5_notrunc_cog_simple_K50BOOT.dta
* final output: tex file: 005_het_beta5_delta2_estimates_wagebefore_table5_notrunc_hours_K50.tex


********************************************************************************
********************************************************************************
// CREATE Figure 7 - Tructation

global subsample  "fullsample"
global modelname ""
global truncation_list "50(10)3000"

do "006_hom_results_LL.do"
do "006_LinearityPlot.do"

* output: dta file: 004_hom_results_fullsample_cog_simple_K50_LL.dta
* final output: pdf file: beta_lin50.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 8 and Figure 9 

global subsample  "fullsample"
do "006_hom_results_ESens.do"

* output: dta file: 002_Fit.dta
* final output: pdf file: b_ESens_hom.pdf and CDF_ESens_hom.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 10 

do "006_hom_byK.do"
do "006_hom_byK_Plot.do"

* output: dta file: 000_hom_byK.dta
* final output: pdf file: b_hom_byK.pdf


********************************************************************************
// CREATE Table 5 (#1 in the appendix)

do "007_AET_Oster.do"

* output: tex file: 
*007_TableAET_Oster.tex


********************************************************************************
// CREATE Table 6 (#2 in the appendix)

*Table 2 in paper
*global subsample "fullsample"
*local subsample "$subsample"
*do "003_cluster.do"
*do "004_hom_results.do"
*do "004_hom_results_BOOT.do"
*do "005_table_hom_results.do"

*Table 2 with married women only
global subsample  "married"
local subsample  "$subsample"
do "003_cluster.do"
do "004_hom_results.do"
do "004_hom_results_BOOT.do"
do "005_table_hom_results.do"

*Table 2 with unmarried women only
global subsample "unmarried"
local subsample "$subsample"
do "003_cluster.do"
do "004_hom_results.do"
do "004_hom_results_BOOT.do"
do "005_table_hom_results.do"

*Table 2 with low education women only
global subsample "loweduc"
local subsample "$subsample"
do "003_cluster.do"
do "004_hom_results.do"
do "004_hom_results_BOOT.do"
do "005_table_hom_results.do"

*Table 2: Measures of non-mother income before birth (N=4,942 with new constraints)
global subsample "non_mom_income"
local subsample "$subsample"
do "003_cluster.do"
do "004_hom_results.do"
do "004_hom_results_BOOT.do"
do "005_table_hom_results.do"

* output: tex file: 
*005_hom_estimates_married_hours_K50.tex
*005_hom_estimates_unmarried_hours_K50.tex
*005_hom_estimates_loweduc_hours_K50.tex
*005_hom_estimates_non_mom_income_hours_K50.tex

*output: use the table results to construct 005_hom_estimates_altests_hours_K50.tex

********************************************************************************
// CREATE Table 7 (#3 in the appendix)

global subsample "fullsample"
local subsample "$subsample"
do "004_hom_beta_flex_delta_results.do"
do "004_hom_beta_flex_delta_results_BOOT.do"
do "005_table_hom_beta_flex_delta_results.do"

* output: tex file: 005_hom_beta_flex_delta_estimates_fullsample_hours_K50.tex


********************************************************************************
// CREATE Table 8 (#4 in the appendix)

global BETA_control_list "afqt_mom hhours hrsafqt" 
global DELTA_control_list "afqt_mom" 
global subsample = "fullsample"
local subsample  "$subsample"
global modelname "table3_main_flexdelta"

do "004_het_beta3_delta1_flex_delta_results.do"
do "004_het_beta3_delta1_flex_delta_results_BOOT.do"
do "005_table_het_beta3_delta1_flex_delta_results.do"

* output: tex file: 005_het_beta3_delta1_flex_delta_estimates_fullsample_table3_main_flexdelta_hours_K50.tex


********************************************************************************
// CREATE Table 9 (#5 in the appendix)

global BETA_control_list "afqt_mom wage_mom_r hhours hrsafqt hrswage_r" 
global DELTA_control_list "afqt_mom wage_mom_r" 
global subsample "wagebefore"
local subsample = "$subsample"
global modelname = "table5_notrunc_flexdelta"

do "004_het_beta5_delta2_flex_delta_results.do"
do "004_het_beta5_delta2_flex_delta_results_BOOT.do"
do "005_table_het_beta5_delta2_flex_delta_results.do"

* output: tex file: 005_het_beta5_delta2_flex_delta_estimates_wagebefore_table5_notrunc_flexdelta_hours_K50.tex


********************************************************************************
********************************************************************************
// CREATE Figure 11 (#1 in the appendix) - Created in Latex


********************************************************************************
********************************************************************************
// CREATE Figure 12 (#2 in the appendix) - Created in Latex


*******************************************************************************
// CREATE Figure 13 (#3 in the appendix)

do "002_histogram_hours_nearzero.do"

* output: pdf file: 002_BunchingPDF_hours.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 14 (#4 in the appendix)

global subsample = "fullsample"
local subsample  "$subsample"
global truncation_list "50(10)3000"

do "006_het_results_LL.do"
do "006_LinearityPlot_het.do"

* output: dta file: 004_het_results_$subsample_cog_simple_K50_LL.dta
* final output: pdf file: beta_lin50_het.pdf and betaA_lin50_het.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 15 (#5 in the appendix)

global subsample "wagebefore"
global truncation_list "50(10)3000"

do "006_het2_results_LL.do"
do "006_LinearityPlot_het2.do"

* output: dta file: 004_het2_results_fullsample_cog_simple_K50_LL.dta
* final output: pdf file: betaW_lin50_het2.pdf and betaA_lin50_het2.pdf 



********************************************************************************
********************************************************************************
// CREATE Figure 16 (#6 in the appendix)

global subsample "fullsample"
do "006_het_ESens.do"

* final output: pdf file: b_ESens_het.pdf and bA_ESens_het.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 17 (#7 in the appendix)

global subsample "wagebefore"
do "006_het2_ESens.do"

* final output: pdf file: bW_ESens_het2 and bA_ESens_het2.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 18 (#8 in the appendix)

global subsample "fullsample"
do "006_het_byK.do"
do "006_het_byK_Plot.do"

* output: dta file: 000_het_byK.dta
* final output: pdf file: bA_het_byK.pdf


********************************************************************************
********************************************************************************
// CREATE Figure 19 (#9 in the appendix)

global subsample "wagebefore"
do "006_het2_byK.do"
do "006_het2_byK_Plot.do"

* output: dta file: 000_het2_byK.dta
* final output: pdf file: bA_het2_byK.pdf

log close